package com.zoe.onelink.doc.init;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

import java.util.Optional;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-01-24
 */
@Slf4j
@RequiredArgsConstructor
public class DocApplicationInfoPrinter implements ApplicationListener<AvailabilityChangeEvent<ReadinessState>> {

    private final Environment environment;

    @Override
    public void onApplicationEvent(AvailabilityChangeEvent<ReadinessState> availabilityChangeEvent) {
        // ReadinessState.ACCEPTING_TRAFFIC 表示可以接受流量
        if (availabilityChangeEvent.getState() == ReadinessState.ACCEPTING_TRAFFIC) {
            try {
                String serverPort = Optional.ofNullable(this.environment.getProperty("local.server.port"))
                        .orElse(this.environment.getProperty("server.port"));
                log.info("======================================================================");
                log.info("本地接口文档地址: http://127.0.0.1:{}/doc.html", serverPort);
                String frmUrl = environment.getProperty("onelink.doc.framework-url", "http://192.168.4.89/docs/onelink-framework");
                log.info("ONE-LINK框架文档地址: {}", frmUrl);
                log.info("======================================================================");
            } catch (Exception e) {
                // ignore
            }
        }
    }

}
